<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Mascota extends MY_Controller {

    private $acciones = array('orden', 'id', 'clase');
    private $globals = array('url_base' => 'anuncio/mascota');
    
    function __construct() {
        parent::__construct();
        $this->lang->load('panel_' . $this->config->item('language'), $this->config->item('language'));
        $this->lang->load('panel_anuncio_' . $this->config->item('language'), $this->config->item('language'));
        $this->load->model('producto_model');
    }
    
    public function index() {
        $this->title = ulang('mascotas').$this->title;
        $this->lang->load('razas_' . $this->config->item('language'), $this->config->item('language'));
        $this->load->library('Fechas');

        $this->data['query'] = $this->producto_model->getProducto();
        if(count($this->data['query']) > 0){
            $this->data['globals'] = $this->globals;
            $this->_renderback($this->globals['url_base'].'/tabla_animales');
        }else{
            //si no hay empresas vamos directamente a crear una
            redirect(PANEL.$this->globals['url_base']."/paso1");
        }
    }
    
    public function paso1(){
        $arreglo = $this->uri->uri_to_assoc(5, $this->acciones);
        if(!$this->producto_model->is_producto_propio($arreglo['id'])){redirect(PANEL.$this->globals['url_base']);}

        $this->title = ulang('mascotas').$this->title;
        $wizard_active = substr(__FUNCTION__, -1, 1);

        if($arreglo['id'] != null){
            $this->data['query'] = reset($this->producto_model->getProducto(array('pro_id' => $arreglo['id'])));
        }
        $this->javascript2[] = PLUGINS . 'jquery-mixitup/jquery.mixitup.min.js';
        $this->javascript2[] = PLUGINS . 'fancybox/source/jquery.fancybox.pack.js';
        $this->css[] = PLUGINS . 'fancybox/source/jquery.fancybox.css';
        $this->css[] = CSSBACK . 'pages/portfolio.css';
        $this->data['subtipos'] = $this->producto_model->getSubtipos(array('pst_fk_prod_tipo' => 1));

        $this->data['wizard'] = $this->load->view($this->globals['url_base'].'/pasos', 
                array('active' => $wizard_active, 'arreglo' => $arreglo, 'globals' => $this->globals), true);
        $this->data['arreglo'] = $arreglo;
        $this->data['globals'] = $this->globals;
        $this->_renderback($this->globals['url_base'].'/wizard_inicio');
    }
    
    public function paso2(){
        $arreglo = $this->uri->uri_to_assoc(5, $this->acciones);
        if(!$this->producto_model->is_producto_propio($arreglo['id'])){redirect(PANEL.$this->globals['url_base']);}
        $this->lang->load('razas_' . $this->config->item('language'), $this->config->item('language'));
        $this->load->library('Fechas');

        $this->title = ulang('mascotas').$this->title;
        $wizard_active = substr(__FUNCTION__, -1, 1);

        if($arreglo['id'] != null && $arreglo['clase'] != null){ //si enviamos la clase con el ID la actualizamos
            $this->producto_model->setClase($arreglo['id'], $arreglo['clase']);
        }

        $this->load->library('form_validation');
        if ($this->form_validation->run('v_datos_producto') == true) {
            $arreglo['id'] = $this->producto_model->setProducto($arreglo['clase']);
            if($this->session->flashdata('error') != ''){
                //si hay un error redireccionamos para que salga el error del flashdata
                redirect(PANEL.$this->globals['url_base']."/paso2/".$this->uri->assoc_to_uri($arreglo));
            }
            if($arreglo['id'] != null){
                redirect(PANEL.$this->globals['url_base']."/paso3/".$this->uri->assoc_to_uri($arreglo));
            }
        }
        $this->data['error'] = validation_errors();

        $idclase = $arreglo['clase'];
        if($arreglo['id'] != null){
            $this->data['query'] = reset($this->producto_model->getProducto(array('pro_id' => $arreglo['id'])));
            $idclase = $this->data['query']['pst_id'];
        }

        $this->data['razas'] = $this->producto_model->getRazas(array('prz_fk_prod_subtipo' => $idclase));
        $this->data['max_razas'] = $idclase < 3 ? 2 : 1; //si es gato o perro podemos seleccionar dos razas, sino solo una
        $this->javascript2[] = PLUGINS . 'select2/select2.min.js';
        $this->javascript2[] = PLUGINS . 'select2/select2_locale_es.js';
        $this->javascript2[] = PLUGINS . 'bootstrap-datepicker/js/bootstrap-datepicker.js';

        //TODO - control de idioma
        $this->javascript2[] = PLUGINS . 'bootstrap-datepicker/js/locales/bootstrap-datepicker.es.js';

        $this->css[] = PLUGINS . 'select2/select2_metro.css';
        $this->css[] = PLUGINS . 'bootstrap-datepicker/css/datepicker.css';
        
        $this->data['wizard'] = $this->load->view($this->globals['url_base'].'/pasos',
                array('active' => $wizard_active, 'arreglo' => $arreglo, 'globals' => $this->globals), true);

        $this->data['arreglo'] = $arreglo;
        $this->data['globals'] = $this->globals;
        $this->_renderback($this->globals['url_base'].'/wizard_info');
    }
    
    public function paso3(){
        $arreglo = $this->uri->uri_to_assoc(5, $this->acciones);
        if(!$this->producto_model->is_producto_propio($arreglo['id'])){redirect(PANEL.$this->globals['url_base']);}
        
        $this->title = ulang('mascotas').$this->title;
        $wizard_active = substr(__FUNCTION__, -1, 1);
        
        if(!empty($_FILES)) {
            $this->load->library('Archivos');
            //preparamos los datos
            $this->data['query'] = reset($this->producto_model->getProducto(array('pro_id' => $arreglo['id'])));
            $info_imagenes = $this->archivos->upl_producto($this->data['query']);
            if(!empty($info_imagenes)){
                $this->data['error'] = $this->producto_model->setFotos($info_imagenes, $arreglo['id']);
            }else{
                $this->data['error'] = ulang('no_se_ha_seleccionado_una_imagen');
            }
        }
        $this->javascript2[] = PLUGINS . 'jquery-mixitup/jquery.mixitup.min.js';
        $this->javascript2[] = PLUGINS . 'fancybox/source/jquery.fancybox.pack.js';
        $this->css[] = PLUGINS . 'fancybox/source/jquery.fancybox.css';
        $this->css[] = CSSBACK . 'pages/portfolio.css';
        //volvemos a llamarlo para traer las nuevas imagenes subidas
        $this->data['query'] = reset($this->producto_model->getProducto(array('pro_id' => $arreglo['id'])));
        
        $this->data['wizard'] = $this->load->view($this->globals['url_base'].'/pasos',
                array('active' => $wizard_active, 'arreglo' => $arreglo, 'globals' => $this->globals), true);

        $this->data['arreglo'] = $arreglo;
        $this->data['globals'] = $this->globals;
        $this->_renderback($this->globals['url_base'].'/wizard_fotos');
    }
    
    public function paso4(){
        $arreglo = $this->uri->uri_to_assoc(5, $this->acciones);
        if(!$this->producto_model->is_producto_propio($arreglo['id'])){redirect(PANEL.$this->globals['url_base']);}
        
        $this->title = ulang('mascotas').$this->title;
        $wizard_active = substr(__FUNCTION__, -1, 1);
        
        
        $this->data['query'] = reset($this->producto_model->getProducto(array('pro_id' => $arreglo['id'])));
        
        $this->javascript2[] = PLUGINS . 'bootstrap-switch/static/js/bootstrap-switch.min.js';
        $this->css[] = PLUGINS . 'bootstrap-switch/static/stylesheets/bootstrap-switch-metro.css';
        $this->data['wizard'] = $this->load->view($this->globals['url_base'].'/pasos',
                array('active' => $wizard_active, 'arreglo' => $arreglo, 'globals' => $this->globals), true);

        $this->data['arreglo'] = $arreglo;
        $this->data['globals'] = $this->globals;
        $this->_renderback($this->globals['url_base'].'/wizard_envio');
    }
    
    public function borrar_imagen($idproducto, $idimagen){
        $this->load->model('empresa_model');
        if(!$this->empresa_model->is_empresa_propia($idproducto)){redirect(PANEL.strtolower(__CLASS__)."/empresas");}
        $this->empresa_model->borrar_imagen($idproducto, $idimagen);
        redirect(PANEL.$this->globals['url_base']."/paso3/".$idproducto);
    }
    
    /*public function activar_empresa($idproducto){
        if($this->session->userdata('usu_fk_rol') < 3){
            $this->load->model('empresa_model');
            $this->empresa_model->setEmpresa(array('emp_estado' => 'A'), null, null, $idproducto);
            redirect(PANEL.strtolower(__CLASS__)."/empresas");
        }
    }
    
    public function desactivar_empresa($idproducto){
        if($this->session->userdata('usu_fk_rol') < 3){
            $this->load->model('empresa_model');
            $this->empresa_model->setEmpresa(array('emp_estado' => 'P'), null, null, $idproducto);
            redirect(PANEL.strtolower(__CLASS__)."/empresas");
        }
    }
    
    public function comprobar_nombre_empresa(){
        $this->load->model('empresa_model');
        $array = array();
        foreach($this->empresa_model->getEmpresa() as $emp){
            $array[] = $emp['emp_nombre'];
        }
        echo json_encode($array);
    }*/
}